package com.wjzjq.hiring.talent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wjzjq.hiring.common.DBObject;
import com.wjzjq.hiring.common.util.DBUtil;
import com.wjzjq.hiring.talent.entity.TalentEntity;

public class TalentDao {
	public TalentEntity login(String code, String password) {
		TalentEntity talent = null;
		DBObject dbObject = DBUtil.executeQuery("SELECT * FROM talent WHERE code=? AND password=?", code, password);
		ResultSet resultSet = dbObject.getResultSet();
		try {
			if (resultSet.next()) {
				talent = new TalentEntity();
				talent.setId(resultSet.getInt("id"));
				talent.setName(resultSet.getString("name"));
				talent.setPhone(resultSet.getString("phone"));
				talent.setAge(resultSet.getInt("age"));
				talent.setPassword(password);
				talent.setEmail(resultSet.getString("email"));
				talent.setGender(resultSet.getInt("gender"));
				talent.setCode(code);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return talent;

	}
	
	public  List<TalentEntity> listAllTalent() {
		// TODO Auto-generated method stub
		String sql = "select * from talent";
		DBObject dbObject = DBUtil.executeQuery(sql);
		ResultSet rs = dbObject.getResultSet();
		List<TalentEntity> talentList = new ArrayList();
		try {
			while (rs.next()) {
				TalentEntity t = new TalentEntity();
				t.setId(rs.getInt("id"));
				t.setName(rs.getString("name"));
				t.setPhone(rs.getString("phone"));
				t.setEmail(rs.getString("email"));
				t.setAge(rs.getInt("age"));
				t.setGender(rs.getInt("gender"));
				t.setHobby(rs.getString("hobby"));
				talentList.add(t);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(dbObject);
		}
		return talentList;
	}

	public int update(TalentEntity talent) {
		int row = 0;

		String sql = "update talent set password=?,name=?,phone=?,email=?,age=?,gender=?,hobby=? where id=?";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(talent.getPassword());
		paramList.add(talent.getName());
		paramList.add(talent.getPhone());
		paramList.add(talent.getEmail());
		paramList.add(talent.getAge());
		paramList.add(talent.getGender());
		paramList.add(talent.getHobby());
		paramList.add(talent.getId());
		row = DBUtil.executeUpdate(sql, paramList);
		return row;
	}

	public int register(TalentEntity talent) {
		String sql = "insert into talent values(null,?,?,?,?,?,?,?,?)";
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(talent.getCode());
		paramList.add(talent.getPassword());
		paramList.add(talent.getName());
		paramList.add(talent.getPhone());
		paramList.add(talent.getEmail());
		paramList.add(talent.getAge());
		paramList.add(talent.getGender());
		paramList.add(talent.getHobby());
		return DBUtil.executeUpdate(sql, paramList);
	}
}
